package com.trkj.controller.wdm;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.pojo.ContractCategory;
import com.trkj.service.ServiceException;
import com.trkj.service.wdm.ContractAdministrationService;
import com.trkj.service.wdm.OneTableService;
import com.trkj.vo.Result2;

@Controller
public class ContractAdministrationController_yj {
	@Autowired
	private ContractAdministrationService conService;
	@Autowired
	private OneTableService oneDao;
	
	/**
	 * 查询合同类型所有
	 * @param page
	 * @param rows
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("ContractCategory_all")
	@ResponseBody
	public Map findAllContractCategory(int page,int rows) throws ServiceException{
		Long total = oneDao.PageContractCategory();
		List<ContractCategory> list = oneDao.findAllContractCategory(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	//合同管理查询
	@RequestMapping("to_ContractManagement")
	public String to_ContractManagement(){
		return "contract_yj";
	}
	
	/**
	 * 分页查询合同所有
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("find-contract_all_yj")
	@ResponseBody
	public Map findAllContract(int page,int rows){
		Page<JSONArray> page1 = PageHelper.startPage(page,rows);
		conService.findAllContract();
		Map map = new HashMap<>();
		map.put("total", page1.getTotal());
		map.put("rows", page1.getResult());
		return map;
	}
	//合同管理新增
	@RequestMapping("to_contract_insert_yj")
	public String to_ContractInsert(){
		return "contract_insert_yj";
	}
	
	
	/**
	 * 查询单条合同类型信息
	 * @param id
	 * @return
	 */
	@RequestMapping("find-one-contract-yj")
	@ResponseBody
	public ContractCategory findOneContract(Long id){
		return conService.findOneContract(id);
	}
	
	
	/**
	 * 新增员工合同信息
	 * @param id	员工id
	 * @param id2 	合同id
	 * @param t		开始时间
	 * @param t2	结束时间
	 * @param t3	合同期限
	 * @param t4	试用结束时间
	 * @param remarks 内容
	 * @param beizhu	备注
	 */
	@RequestMapping("insert_contract_admin_yj")
	@ResponseBody
	public Result2 insertContractAdmin(Long id,Long id2,Timestamp t,Timestamp t2,Timestamp t3,Timestamp t4,String remarks,String beizhu){
		 try {
			conService.insertContractAdmin(id, id2, t, t2, t3, t4, remarks, beizhu);
			 return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
	}
	
	/**
	 * 模糊分页查询合同所有
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("findlike-contract_all_yj")
	@ResponseBody
	public Map findLikeAllContract(String name,int page,int rows){
		Page<JSONArray> page1 = PageHelper.startPage(page,rows);
		conService.findLikeAllContract(name);
		Map map = new HashMap<>();
		map.put("total", page1.getTotal());
		map.put("rows", page1.getResult());
		return map;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("delete_contract_admin_yj")
	@ResponseBody
	public Result2 deleteContractAdmin(Long id){
		 try {
			conService.deleteContract(id);
			 return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
	}
	
	/**
	 * 合同管理修改
	 * @return
	 */
	@RequestMapping("to_contract_update_yj")
	public String to_ContractUpdate(Long id,Model m){
		m.addAttribute("con", conService.findOneContractAdministration(id));
		return "contract_update_yj";
	}
	
	/**
	 * 编辑员工合同信息
	 * @param id	员工id
	 * @param id2 	合同id
	 * @param t		开始时间
	 * @param t2	结束时间
	 * @param t3	合同期限
	 * @param t4	试用结束时间
	 * @param remarks 内容
	 * @param beizhu	备注
	 */
	@RequestMapping("update_contract_admin_yj")
	@ResponseBody
	public Result2 updateContractAdmin(Long id,Long id1,Long id2,Timestamp t,Timestamp t2,Timestamp t3,Timestamp t4,String remarks,String beizhu){
		 try {
			conService.updateContractAdministration(id, id1, id2, t, t2, t3, t4, remarks, beizhu);
			 return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
	}
	
}
